package com.woniu108.evaluate.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.evaluate.dto.ProductScoreDto;
import com.woniu108.evaluate.form.ProductScoreAddForm;
import com.woniu108.evaluate.model.ProductScore;
import com.woniu108.evaluate.param.ProductScoreAddParam;
import com.woniu108.evaluate.service.ProductScoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品评分表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/productScore")
@Api(tags = "商品评分接口")
public class ProductScoreController {
    @Resource
    public ProductScoreService productScoreService;
    @Resource
    private RedisUtil redisUtil;

    public String getUserIdByToken(String token){
        String refreshToken = redisUtil.getString(token);
        return JWTUtil.parseUserToken(refreshToken).get("userId").toString();
    }
    /**
     * "查询某个商品的评分"
     * @param pItemId 商品id
     * @return 单个商品的评分
     */
    @GetMapping("/queryProductScore/{pItemId}.c")
    @ApiOperation("查询某个商品的评分")
    public Result<ProductScoreDto> queryProductScore(@PathVariable("pItemId")@ApiParam("商品id") Integer pItemId){
        ProductScoreDto productScore = productScoreService.selectProductScoreById(pItemId);
        return Result.getResult(ProductScoreDto.class).setData(productScore);
    }

    /**
     * "为商品评分（用户）"
     * @param form 商品订单编号 评分
     * @param token userID
     * @return boolean值
     */
    @PostMapping("/addScore")
    @ApiOperation("为商品评分（用户）")
    public Result<Boolean> addScore(@RequestBody ProductScoreAddForm form, @RequestHeader String token){
        String userId = getUserIdByToken(token);
        ProductScoreAddParam param = BeanUtil.toBean(form, ProductScoreAddParam.class);
        param.setUserId(userId);

        productScoreService.addScore(param);
        return Result.getResult(Boolean.class).setMsg("评分成功").setData(true);
    }

    /**
     * 创建商品的评分信息
     * @param pItemId 商品编号
     * @return boolean值
     */
    @GetMapping("/createProductScoreByPItemId/{pItemId}.api")
    @ApiOperation("创建商品的评分信息")
    @AdminHistoryLog
    public Result<Boolean> createProductScoreByPItemId(@PathVariable("pItemId")@ApiParam("商品id") Integer pItemId){
        Boolean aBoolean=productScoreService.addProductScoreByPItemId(pItemId);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("创建成功");
    }

    /**
     * 商品删除时，删除其评价信息
     * @param pItemId 商品id
     * @return 布尔值
     */
    @GetMapping("/delProductScore/{pItemId}.api")
    @ApiOperation("商品删除时，删除其评价信息")
    @AdminHistoryLog
    public Result<Boolean> delProductScore(@PathVariable("pItemId")@ApiParam("商品id")Integer pItemId){
        Boolean aBoolean=productScoreService.delProductScore(pItemId);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("删除评价记录成功");
    }

    /**
     * 查询所有商品评分
     * @return productScoreDtos
     */
    @GetMapping("/queryAllProductScore.c")
    @ApiOperation("查询所有商品评分")
    public Result<List<ProductScoreDto>> queryAllProductScore(){
        List<ProductScoreDto> productScoreDtos=productScoreService.selectAllProductScore();
        return Result.newListInstance(ProductScoreDto.class).setData(productScoreDtos).setMsg("查询所有商品评分");
    }
}

